//
//  Bundle.swift
//  StandardKit
//
//  Created by 李加建 on 2021/10/13.
//

import Foundation

public extension Bundle {
    // 读取pod 内 的 bundle
    static func bundle(_ bundleName: String, podName: String) -> Bundle? {
        var associateBundleURL = Bundle.main.url(forResource: "Frameworks", withExtension: nil)
        associateBundleURL = associateBundleURL?.appendingPathComponent(podName)
        associateBundleURL = associateBundleURL?.appendingPathExtension("framework")
        guard let associateBundleURL2 = associateBundleURL else { return nil }
        let associateBundle = Bundle(url: associateBundleURL2)
        guard let newAssociateBundleURL = associateBundle?.url(forResource: bundleName, withExtension: "bundle") else { return nil }
        let bundle = Bundle(url: newAssociateBundleURL)
        return bundle
    }

    // 读取pod 内 的 bundle podName 和 bundleName同名
    static func bundle(podName: String) -> Bundle? {
        var associateBundleURL = Bundle.main.url(forResource: "Frameworks", withExtension: nil)
        associateBundleURL = associateBundleURL?.appendingPathComponent(podName)
        associateBundleURL = associateBundleURL?.appendingPathExtension("framework")
        guard let associateBundleURL2 = associateBundleURL else { return nil }
        let associateBundle = Bundle(url: associateBundleURL2)
        guard let newAssociateBundleURL = associateBundle?.url(forResource: podName, withExtension: "bundle") else { return nil }
        let bundle = Bundle(url: newAssociateBundleURL)
        return bundle
    }

    // 读取main 的 bundle
    static func bundle(_ bundleName: String) -> Bundle? {
        guard let associateBundleURL = Bundle.main.url(forResource: bundleName, withExtension: "bundle") else { return nil }
        return Bundle(url: associateBundleURL)
    }
}
